﻿using ProjectFootball.MVC.Areas.CountryArea.Models;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public static class _CountryExtensions
    {
        public static CountryDataModel GetDataModel(this Country country)
        {
            return new CountryDataModel()
            {
                CountryGuid = country.Pk,
                ApprovalGuid = country.ApprovalGuid,
                CountryName = country.CountryName,
                ActiveFrom = country.ActiveFrom,
                ActiveTo = country.ActiveTo
            };
        }

        public static IEnumerable<CountrySearchResultViewModel> Search(this IQueryable<Country> countries, IPrincipal user, SearchType searchType)
        {
            var query = countries.Where(c => !c.IsMarkedForDeletion).AsEnumerable().Where(c => c.CanView(user)).OrderBy(c => c.CountryName);

            foreach (Country country in query)
                yield return country.GetSearchResultViewModel(user, searchType);
        }

        public static CountrySearchResultViewModel GetSearchResultViewModel(this Country country, IPrincipal user, SearchType searchType)
        {
            return new CountrySearchResultViewModel()
            {
                Country = country,
                IsVisible = country.CanView(user),
                IsEditVisible = searchType == SearchType.Details ? country.CanView(user) : false,
                IsDeleteVisible = (searchType == SearchType.Details || (searchType == SearchType.RecordForApproval && !country.IsApproved)) ? country.CanDelete(user) : false,
                IsViewApprovalsVisible = (searchType == SearchType.Details || searchType == SearchType.PendingApprovals) ? country.CanApprove(user) : false,
                IsConfirmApprovalVisible = searchType == SearchType.RecordForApproval ? country.CanApprove(user) : false
            };
        }

        public static void Edit(this Country country, CountryDataModel countryDataModel, IPrincipal user)
        {
            country.ApprovalGuid = countryDataModel.ApprovalGuid;
            country.CountryName = countryDataModel.CountryName;
            country.ActiveFrom = countryDataModel.ActiveFrom;
            country.ActiveTo = countryDataModel.ActiveTo;
            country.IsApproved = country.IsApproved ? user.IsApprover() : false;
            country.AuditUserId = user.GetUserId();
            country.AuditTimestamp = DateTime.Now;
        }

        public static void Approve(this Country country, Country previousApproved)
        {
            previousApproved.CountryName = country.CountryName;
            previousApproved.ActiveFrom = country.ActiveFrom;
            previousApproved.ActiveTo = country.ActiveTo;
            previousApproved.AuditUserId = country.AuditUserId;
            previousApproved.AuditTimestamp = country.AuditTimestamp;
        }
    }
}